package com.pentalog.ginet;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.pentalog.ginet.domain.Employee;
import com.pentalog.ginet.domain.Star;
import com.pentalog.util.HibernateUtil;

/**
 * Hello world!
 *
 */
public class TestHSQL 
{
    public static void main( String[] args )
    {
    	TestHSQL hsql = new TestHSQL();
    	hsql.listStars();
//    	hsql.listEmployees();
    }
    private void listStars() {
		Session session = HibernateUtil.getSessionFactoryHSQL().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			List stars = session.createQuery("FROM Star").list();
			for (Iterator iterator = stars.iterator(); iterator.hasNext();) {
				Star star = (Star) iterator.next();
				System.out.print(star.getId() + "\tFirst Name:\t"
						+ star.getFirstName());
				System.out.println("\tLast Name:\t" + star.getLastName());
			}
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
    private void listEmployees() {
    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			List employees = session.createQuery("FROM Employee").list();
			for (Iterator iterator = employees.iterator(); iterator.hasNext();) {
				Employee employee = (Employee) iterator.next();
				System.out.print(employee.getId() + "\tFirst Name:\t"
						+ employee.getFirstName());
				System.out.print("\tLast Name:\t" + employee.getLastName());
				System.out.println("\tSalary:\t" + employee.getSalary());
			}
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
}
